﻿using System.Collections.Generic;
using System.Linq;
using Dapper;

namespace ExtendLib.Helper
{
    /// <summary>
    ///软件一些控制 配置
    /// </summary>
    [Table("OtherConfig")]
    public class ControlModel
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 控制类型
        /// </summary>
        public string OtherType { get; set; }
        /// <summary>
        /// 控制状态  正常、废弃 （如果是废弃 即 为停止某功能）
        /// </summary>
        public string OtherStatus { get; set; }
    }
    /// <summary>
    /// 控制类型的枚举  如果加功能 此枚举也要加
    /// </summary>
    public enum EOtherType
    {
        空,
        代理,
        发送状态,
        是否启用代理
    }

    /// <summary>
    /// 控制状态帮助类
    /// </summary>
    public class ControlHelper
    {
        /// <summary>
        /// 给定类型 获取状态 
        /// 正常=>true  废弃=>false
        /// </summary>
        /// <param name="type">控制类型</param>
        public static bool GetStatus(EOtherType type)
        {
            var list = DbHelperByDapper.GetList<ControlModel>(new { OtherType = type.ToString() }).ToList();
            if (list.Any() == false) //找不到 就不能禁用此功能
                return true;
            var model = list[0];
            if (model.OtherStatus == "废弃")
                return false;
            return true;
        }
     
        /// <summary>
        /// 根据类型 更新状态
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="status">默认正常</param>
        /// <returns></returns>
        public static bool SetStatus(EOtherType type, string status = "正常")
        {
            var list = DbHelperByDapper.GetList<ControlModel>(new { OtherType = type.ToString() }).ToList();
            if (list.Any() == false) //找不到
                return false;
            var model = list[0];
            model.OtherStatus = status;
            return DbHelperByDapper.ModifyDb(model)>0;
        }
        /// <summary>
        /// 根据类型 获取所有数据
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<ControlModel> GetListByType(EOtherType type)
        {
            object where = new {};
            if (type != EOtherType.空)
            {
                where = new { OtherType = type.ToString()};
            }
            return DbHelperByDapper.GetList<ControlModel>(where).ToList();
        }
        /// <summary>
        /// 保存代理ip 用于自动切换节点
        /// </summary>
        /// <param name="listProxy"></param>
        /// <returns></returns>
        public static bool SaveProxyIp(List<string> listProxy)
        {
            List<ControlModel> list= listProxy.Select(item => new ControlModel
            {
                OtherType = "代理", OtherStatus = item
            }).ToList();
            return DbHelperByDapper.InsertDb(list);
        }
        /// <summary>
        /// 根据类型 批量删除
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool BatchDelete(EOtherType type)
        {
           return DbHelperByDapper.DeleteDbList<ControlModel>(new {OtherType = type.ToString()})>0;
        }
        /// <summary>
        /// 根据主键删除对应代理
        /// </summary>
        /// <param name="keyId"></param>
        /// <returns></returns>
        public static bool Delete(int keyId)
        {
            return DbHelperByDapper.DeleteDb<ControlModel>(keyId) > 0;
        }
    }
}
